{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU",
    "gpuClass": "standard"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Setup and Installation"
      ],
      "metadata": {
        "id": "Urt59paA7mWe"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "-8rxVjQcO5O_"
      },
      "outputs": [],
      "source": [
        "!nvidia-smi"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Install ultralytics, yolov8\n",
        "!pip install ultralytics\n",
        "from IPython import display\n",
        "display.clear_output()\n",
        "\n",
        "import ultralytics\n",
        "ultralytics.checks()"
      ],
      "metadata": {
        "id": "eDFN0mi8PVvA"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from ultralytics import YOLO\n",
        "\n",
        "from IPython.display import display, Image"
      ],
      "metadata": {
        "id": "THPbr6iF6pzt"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Mount google drive for access to data\n",
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "metadata": {
        "id": "PwGGa3z8Pc0q"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Detection"
      ],
      "metadata": {
        "id": "Nse_iHNb7S51"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "model_det = YOLO(\"yolov8n.pt\")"
      ],
      "metadata": {
        "id": "bb0Kabl6nmwA"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# change path to drive folder with detection data\n",
        "%cd /content/drive/MyDrive/Analogue Gauge Reading/data_splits/detection"
      ],
      "metadata": {
        "id": "h4wT7MIcoUnx"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# train model\n",
        "model_det.train(task='detect', data='data.yaml', plots=True, epochs=5, conf=0.4)"
      ],
      "metadata": {
        "id": "cNYQPs6odjxv"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#validate model\n",
        "metrics = model_det.val()\n",
        "metrics"
      ],
      "metadata": {
        "id": "gwN-SLIEdkEM"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Export the model\n",
        "model_det.export(format=\"onnx\")"
      ],
      "metadata": {
        "id": "I930uU_ufWdO"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!ls runs/detect/train/"
      ],
      "metadata": {
        "id": "7oFjBy6zpSbX"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "Image(filename='runs/detect/train/confusion_matrix.png', width=600)"
      ],
      "metadata": {
        "id": "FmQSnYsc-p6I"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "Image(filename='runs/detect/train/results.png', width=600)\n"
      ],
      "metadata": {
        "id": "_OqDgQak-tD6"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "Image(filename='runs/detect/train/val_batch0_pred.jpg', width=600)"
      ],
      "metadata": {
        "id": "8sSztSwO-4OE"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#inference\n",
        "!yolo task=detect mode=predict model=runs/detect/train/weights/best.pt conf=0.25 source=test/images"
      ],
      "metadata": {
        "id": "BZjcODEt_Ifb"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Segmentation"
      ],
      "metadata": {
        "id": "ULenhEAc7Y-g"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "%cd /content/drive/MyDrive/Analogue Gauge Reading/data_splits/segmentation"
      ],
      "metadata": {
        "id": "uq2qkkUM-qSt"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!yolo task=segment mode=train model=yolov8n-seg.pt data= data.yaml epochs=5 plots=True"
      ],
      "metadata": {
        "id": "-a7V49Jq-qVd"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "Image(filename='runs/segment/train/results.png', width=600)\n"
      ],
      "metadata": {
        "id": "zKzdi5Pz-qYy"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "Image(filename='runs/segment/train/val_batch0_pred.jpg', width=600) "
      ],
      "metadata": {
        "id": "XdsTnKIjDYN3"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!yolo task=segment mode=val model=runs/segment/train/weights/best.pt data=data.yaml"
      ],
      "metadata": {
        "id": "NT7FadD7OELu"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}
